/**
 * @author jMz
 * @date 2021/9/18 0018
 */
package com.shizhan.first.controller;

import com.shizhan.first.entity.Student;
import com.shizhan.first.mapper.StudentMapper;
import com.shizhan.first.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.List;

@Controller
//@RestController
@RequestMapping("stu")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("{id}")
    @ResponseBody
    public Student queryStudentById(@PathVariable("id") Long id) {
        return this.studentService.queryById(id);
    }

    /**
     * 往表中增加一条数据
     */


    @PostMapping("/insertOne")
    public Student insertOne(@RequestParam("id") Long id,
                            @RequestParam("name") String name,
                            @RequestParam("age") Integer age) {

        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        int result = studentService.insertStudent(student);
        System.out.println("插入的结果是：" + result);
        return student;
    }

//mapper自定义查询学生
    @GetMapping("/allStu")
//    @ResponseBody
    public String allStu(Model model){
        List<Student> students = this.studentService.getStudent();
        model.addAttribute("students",students);
        return "students";
    }

    @GetMapping("/all")
    public String all(Model model) {

        //查询学生
        List<Student> students = this.studentService.queryStudentAll();
        //放入模型
        model.addAttribute("students", students);
        //返回模板名称（就是classpath:/templates/目录下的html文件名）
        return "students";
    }

    @GetMapping
    public List<Student> getStudent(){
//        return StudentService.getStudent();
        return studentService.getStudent();
    }

    @DeleteMapping("{id}")
    public int deleteById(@PathVariable("id") Long id){
        return studentService.deleteById(id);
    }


    @GetMapping("/test")
    public String test() {
        return "test";
    }


}
